package com.hmsm.sys.action;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.servlet.ModelAndView;

import com.hmsm.sys.bean.LocalUser;
import com.hmsm.sys.controller.LoginController;
import com.lzyyj.common.BeanUtils;
import com.lzyyj.gmis.base.model.IUser;
import com.lzyyj.gmis.base.webui.ICurrentUserAware;
import com.lzyyj.gmis.base.webui.action.LoginAction;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 授权检查拦截器，检查回话是否已进行身份验证，未验证的话检查是否有已登录了令牌，有的话转换为已登录，否则转入登录页面
 * @author Lzyyj
 */
@SuppressWarnings("serial")
public class AuthorizationInterceptor extends AbstractInterceptor {

	private static final Log log = LogFactory.getLog(AuthorizationInterceptor.class);

	@Resource
	private LoginController loginController;

	public void setLoginController(LoginController loginController){
		this.loginController = loginController;
	}
	
	public LoginController getLoginController() {
		return this.loginController;
	}
	
	/**
	 * session 中当前用户 key
	 */
	private String currentUserKey = "CurrentUser";

	public String getCurrentUserKey() {
		return currentUserKey;
	}

	public void setCurrentUserKey(String currentUserKey) {
		this.currentUserKey = currentUserKey;
	}

	@Override
	public String intercept(ActionInvocation ai) throws Exception {
		Map<String, Object> session = ai.getInvocationContext().getSession();

		Object currentUser = session.get(currentUserKey);


		// 获取HttpServletRequest对象
		HttpServletRequest request = ServletActionContext.getRequest();
		
		// 检查检查腾讯通登录令牌
		String userName =  request.getParameter("userName");
		String syncData =  request.getParameter("syncData");
		String syncurl = request.getParameter("syncurl");
		Object user = null;
		if(syncurl==null){
			// 瑞宏升统一登录
			if(userName!=null&&userName.length()>0&&syncData!=null&&syncData.length()>12){
				ModelAndView modelAndView = loginController.rhsLogin(userName, syncData, syncurl, request);
				user = modelAndView.getModel().get("currentUser");
				
				log.info(userName+"换令牌！");
			}
		}else{
			// 本系统同步登录
			if(userName!=null&&userName.length()>0&&syncData!=null&&syncData.length()>12){
				ModelAndView modelAndView = loginController.syncLogin(userName, syncData, syncurl, request);
				user = modelAndView.getModel().get("currentUser");
				
				log.info(userName+"换令牌！");
			}
		}
		if(user!=null){
			currentUser = user;
		}
		// 对LoginAction,LogoutAction不做该项拦截

		Object action = ai.getAction();
		if (currentUser == null) {
			if (!(action instanceof LoginAction)) {
				if(currentUser ==null){

					// 获取此请求的地址，请求地址包含application name，进行subString操作，去除application
					// name
					String path = request.getRequestURI();
					String servletPath = request.getServletPath();
					String basePath = request.getContextPath();

					// 拼凑得到登陆之前的地址
					String realPath;

					// 获得请求中的参数
					String queryString = request.getQueryString();
					if (queryString == null) {
						realPath = servletPath;
					}else{
						realPath = servletPath + "?" + queryString;
					}
					
					// 存入session，方便调用
					session.put("prePage", realPath);
					
					log.info("未登录！");

					return Action.LOGIN;
				}
			}
		} else {
			if (action instanceof ICurrentUserAware && currentUser instanceof LocalUser) {
				BeanUtils.setProperty(action, "currentUser", currentUser);
			}
		}
		return ai.invoke();

	}
}